const { ccclass, property } = cc._decorator;
import RandomPropCOntrol from './RandomPropControl';

@ccclass
export default class PickupControl extends cc.Component {//拾取技能
    @property(cc.Node)
    skillNode: cc.Node = null;
    @property
    awayTime: number = 0;
    @property(cc.SpriteFrame)
    _skillSprite: cc.SpriteFrame = null;
    @property(cc.Node)
    _skillBtNode: cc.Node = null;
    @property(cc.String)
    _skillId: string = "";
    @property
    _RandomProp: RandomPropCOntrol = null;
    _isGather: boolean = false;

    onLoad() {
        this.skillNode.getComponent(cc.Sprite).spriteFrame = this._skillSprite;
        this._isGather = true;
        this.scheduleOnce(() => {
            this.node.destroy();
        }, this.awayTime);
        this.scheduleOnce(() => {
            this._isGather = false;
            cc.tween(this.node)
                .repeatForever(
                    cc.tween().by(1, { position: cc.v3(0, 10, 0) })
                        .by(1, { position: cc.v3(0, -10, 0) })
                )
                .start();
        }, 0.5);
    }
    onCollisionStay(other, self) {
        if (this._isGather)
            return;
        if (other.tag == TagHeroBody) {
            this._isGather = true;
            this.gatherHero();
        }
        else if (other.tag == TagMonsterBody) {
            this._isGather = true;
            this.gatherMonster(other.node);
        }
    }
    gatherHero() {
        this._RandomProp.addHeroBt(this.node, this._skillId);
    }
    gatherMonster(monster: cc.Node) {
        this._RandomProp.addMonsterBt(this.node, monster, this._skillId);
    }
}
